/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */ 
#include "tbox/tbox.h"


void test_hash_map();
void test_hash_map_class();
void test_refbase();

/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */ 
tb_int_t main(tb_int_t argc, tb_char_t** argv)
{
    // init tbox
    if (!tb_init(tb_null, tb_null)) return -1;

    // trace
    tb_trace_i("hello tbox!");
    
    //test_hash_map();

    //test_hash_map_class();

    test_refbase();

    // exit tbox
    tb_exit();
    return 0;
}

void test_hash_map()
{
    tb_trace_i("------ hash_map -----------");
    //init
    tb_hash_map_ref_t hash=tb_hash_map_init(0,tb_element_str(tb_true),tb_element_str(tb_true));

    //插入
    tb_hash_map_insert(hash,"1","item1");
    tb_hash_map_insert(hash,"2","item2");
    tb_hash_map_insert(hash,"3","item3");
    tb_hash_map_insert(hash,"4","item4");
    tb_hash_map_insert(hash,"5","item5");

    tb_trace_i("size:%d",tb_hash_map_size(hash));
    tb_trace_i("maxn:%d",tb_hash_map_maxn(hash));

    tb_hash_map_dump(hash);

    //查找
    tb_size_t itor = tb_hash_map_find(hash, "3");
    if (itor != tb_iterator_tail(hash))
    {
        // get data
        tb_hash_map_item_ref_t item = (tb_hash_map_item_ref_t)tb_iterator_item(hash, itor);
        tb_assert(item);
        tb_trace_i("%s->%s",item->name,item->data);
        // remove it
        tb_iterator_remove(hash, itor);
    }

    tb_trace_i("size:%d",tb_hash_map_size(hash));
    tb_trace_i("maxn:%d",tb_hash_map_maxn(hash));

    tb_hash_map_dump(hash);

    //获取
    tb_char_t* data = (tb_char_t*)tb_hash_map_get(hash, "4");
    if (data)
    {
        tb_trace_i("%s",data);
    }

    //size and maxn
    tb_trace_i("size:%d",tb_hash_map_size(hash));
    tb_trace_i("maxn:%d",tb_hash_map_maxn(hash));

    tb_hash_map_dump(hash);

    //迭代遍历
    for(itor = tb_iterator_head(hash);itor != tb_iterator_tail(hash);itor = tb_iterator_next(hash,itor)){
        tb_hash_map_item_ref_t item = (tb_hash_map_item_ref_t)tb_iterator_item(hash, itor);
        tb_assert(item);
        tb_trace_i("%s->%s",item->name,item->data);
    }

    tb_hash_map_exit(hash);
}

#include "RefBase.h"
using namespace edui;
class Test : public RefCountedBase
{
public:
    Test(){ tb_char_t s[256] = {0}; tb_snprintf(s, 256, "%d", this); Test((const char*)s);}
    Test(const char* str)
    {
        tb_strncpy(m_str,str,255);
        m_str[255] = 0;
    }
    ~Test(){}

    void print(const char* str)
    {
        tb_trace_i("%s",str);
    }

    void print()
    {
        tb_trace_i("%s,%d",m_str,this);
    }

    char m_str[256];


    REF_IMPLEMENT_COUNTING(Test);

};

void test_refbase()
{
    RefCountedPtr<Test> test1 = new RefCountedObject<Test>();
    RefCountedPtr<Test> test2 = new RefCountedObject<Test>("aaa");

    test1->print();
    test2->print();

    void *p = test2;
    RefCountedPtr<Test> test3((Test*)p);

    test3->print();
}

void test_hash_map_class()
{
    tb_trace_i("------ hash_map class -----------");
    //init
    tb_hash_map_ref_t hash=tb_hash_map_init(0,tb_element_long(),tb_element_ptr(tb_null,tb_null));

    //插入
    for(tb_size_t i=1; i <= 10; ++i){
        RefCountedPtr<Test> test = new RefCountedObject<Test>();
        tb_hash_map_insert(hash,(tb_pointer_t)i,test);
    }

    tb_trace_i("size:%d",tb_hash_map_size(hash));
    tb_trace_i("maxn:%d",tb_hash_map_maxn(hash));

    tb_hash_map_dump(hash);

    //查找
    tb_size_t itor = tb_hash_map_find(hash, (tb_pointer_t)3);
    if (itor != tb_iterator_tail(hash))
    {
        // get data
        tb_hash_map_item_ref_t item = (tb_hash_map_item_ref_t)tb_iterator_item(hash, itor);
        tb_assert(item);
        tb_trace_i("%s->%d",item->name,item->data);
       // RefCountedPtr<Test> test = item->data;
        //test->print(item->name);
        // remove it
        tb_iterator_remove(hash, itor);
    }

    tb_trace_i("size:%d",tb_hash_map_size(hash));
    tb_trace_i("maxn:%d",tb_hash_map_maxn(hash));

    tb_hash_map_dump(hash);

    //获取
    tb_char_t* data = (tb_char_t*)tb_hash_map_get(hash, (tb_pointer_t)4);
    if (data)
    {
        tb_trace_i("%d",data);
        //data->print("item:4,find");
    }

    //size and maxn
    tb_trace_i("size:%d",tb_hash_map_size(hash));
    tb_trace_i("maxn:%d",tb_hash_map_maxn(hash));

    tb_hash_map_dump(hash);

    //迭代遍历
    for(itor = tb_iterator_head(hash);itor != tb_iterator_tail(hash);itor = tb_iterator_next(hash,itor)){
        tb_hash_map_item_ref_t item = (tb_hash_map_item_ref_t)tb_iterator_item(hash, itor);
        tb_assert(item);
        tb_trace_i("%s->%d",item->name,item->data);
       // item->data->print(item->name);
    }

    tb_hash_map_exit(hash);
}